package ar.com.angrymachine.hunter.activities;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Vector;

import ar.com.angrymachine.hunter.Activity;
import ar.com.angrymachine.hunter.Game;
import ar.com.angrymachine.hunter.GraphicStyles;
import ar.com.angrymachine.hunter.Locale;
import ar.com.angrymachine.hunter.gameplay.Room;

public class ExploreActivity extends Activity
{
    private class Discovery
    {
        Discovery(Activity theActivity, int theTime)
        {
            time = theTime;
            activity = theActivity;
        }
        public boolean inTime(int elapsed)
        {
            return elapsed >= time;
        }
        int time;
        Activity activity;
    }
    public int maxExploration;
    public int currentExploration = 0;
    Vector discoveries = new Vector();
    protected Room room;
    
    public ExploreActivity(Room room, int explorationTime)
    {
    	this.room = room;
        maxExploration = explorationTime;
    }
    
    public void addDiscovery(Activity activity, int time)
    {
        discoveries.add(new Discovery(activity,time));
    }
    
    private void processDiscoveries()
    {
        for (int i = 0; i < discoveries.size();)
        {
            Discovery discovery = (Discovery) discoveries.elementAt(i);
            if(discovery.inTime(currentExploration))
            {
                discoveries.removeElementAt(i);
                // process the discovery
                Game.current.currentLevel.addActivity(this, discovery.activity);
                // stop the continuum
                Game.current.resetActivity();
            }
            else
            {
                i++;
            }
        }
    }
    
    public String getDescription()
    {
        return Locale.percentageLabel(room.description, currentExploration, maxExploration);
    }

    public String getActionText()
    {
        if(isCompleted())
        {
            return Locale.EXPLORED;
        }
        else
        {
            if(isRunning())
            {
                return Locale.EXPLORING;
            }
            else
            {
                return Locale.EXPLORE;    
            }
        }
    }
    
    public boolean isCompleted()
    {
        return currentExploration >= maxExploration;
    }

    public void update(int delta, boolean action, boolean timeIsRunning)
    {
        if(timeIsRunning)
        {
            if(action)
            {
                currentExploration += delta * Game.current.player.getExplorationSkill();
                processDiscoveries();
                if(currentExploration >= maxExploration)
                {
                	currentExploration = maxExploration;
                	end(true);
                }
            }
        }
    }

    public void paint(Graphics g, Rectangle activityArea)
    {
        GraphicStyles.paintDefaultActivity(g, this, activityArea, room.icon);
    }
}
